package cn.ywrby.service.impl;

import cn.ywrby.domain.Apply;
import cn.ywrby.domain.ApplySubType;
import cn.ywrby.domain.ApplyType;
import cn.ywrby.mapper.ApplyMapper;
import cn.ywrby.service.ApplyService;
import cn.ywrby.utils.RegularUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    ApplyMapper applyMapper;


    @Override
    public List<ApplyType> getAllApplyTypes() {
        List<ApplyType> applyTypeList=applyMapper.getAllApplyTypes();

        for(ApplyType type: applyTypeList){
            List<ApplySubType> subTypeList=applyMapper.getSubTypeList(type.getTypeId());
            type.setSubTypeList(subTypeList);
        }
        return applyTypeList;
    }

    @Override
    public List<Apply> getApplyList(String userId,Integer page,Integer pageSize,String type) {

        //尝试获取一级类型与二级类型
        List<String> applyType = RegularUtils.getApplyType(type);
        String firstType=applyType.get(0);
        String secondType=applyType.get(1);
        String typeId=applyMapper.getTypeId(firstType);

        //获取分页插件对象
        PageHelper pageHelper=new PageHelper();
        //开始分页，指定分页参数
        PageMethod.startPage(page,pageSize);
        List<Apply> applyList=new ArrayList<>();
        //判断一级类型与二级类型是否为空，为空则不作为检索条件
        if(typeId==null||secondType==null) {
            applyList= applyMapper.getApplyList(userId);
        }else {
            applyList=applyMapper.getApplyListByType(userId,typeId,secondType);
        }
        return applyList;
    }
}
